package com.winning.vsts.merge;

import com.google.common.collect.Sets;
import com.google.common.eventbus.EventBus;
import com.winning.vsts.merge.service.domain.VstsProject;
import com.winning.vsts.merge.service.entity.PullRequestDO;
import com.winning.vsts.merge.service.service.VstsProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 工作项相关测试
 *
 * @author xcl <xcl@winning.com.cn>
 * @date 2021/11/18
 */
@EnableJpaRepositories(basePackages = "com.winning.vsts.merge.service.jpa")
@SpringBootApplication(scanBasePackages = "com.winning.vsts.merge.service")
public class WorksItemApplication implements ApplicationRunner {

    @Autowired
    private VstsProjectService vstsProjectService;

    @Autowired
    private EventBus eventBus;


    public static void main(String[] args) {
        new SpringApplicationBuilder()
            .main(WorksItemApplication.class)
            .sources(WorksItemApplication.class)
            .web(WebApplicationType.NONE)
            .build().run(args);
    }


    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<VstsProject> vstsProjects = vstsProjectService.queryVstsProject("winning-amts-finance-account", null);
        VstsProject vstsProject = vstsProjects.get(0);
        PullRequestDO pullRequestDO = new PullRequestDO();
        pullRequestDO.setVstsProject(vstsProject);
        pullRequestDO.setWorksItemIds(Sets.newHashSet("102214"));
        pullRequestDO.setPullNo("111111111");
        eventBus.post(pullRequestDO);

        // 停1分钟给多线程处理
        TimeUnit.MINUTES.sleep(1);
    }
}
